<?php

/**
 * 定义 Controller_BoService 类
 *
 * @copyright Copyright (c) 2004 - 2009 iTeam
 * @author iTeam Monkey
 * @package XingYaoDianZi
 * @version 200912241123
 */
FLEA::loadClass('Controller_BoBase');

class Controller_BoVideo extends Controller_BoBase {

    var $_modelClasses;
    var $_modelArticles;
    var $access;
    var $catid;
    var $_helperFunction;
    var $dbo;

    /**
     * 构造函数
     *
     * @return Controller_BoGeneral
     */
    function Controller_BoVideo() {
        parent::Controller_BoBase();
        $this->access = NEW_ADMIN;
        $this->catid = VIDEO_CLASS;
        //用户角色权限控制
        if (!$this->checkDispatcher($this->access)) {
            redirect(url('BoDashboard', 'ErrorIn'));
        }
        $this->_modelClasses = & FLEA::getSingleton('Model_Categories');
        $this->_modelArticles = & FLEA::getSingleton('Model_Articles');
        $this->_helperFunction = & FLEA::getSingleton('Helper_Function');
        $this->dbo = &FLEA::getDBO();
    }

    /**
     * 显示文章列表
     */
    function actionIndex() {
        $catid = $this->catid;
        $field = array('class_id' => $catid);
        if (is_numeric($_REQUEST['pcid']) && $_REQUEST['pcid'] > 0)
            $field['pcid'] = $_REQUEST['pcid'];
        if ($_REQUEST['title'])
            $field[] = "title like '%" . $_REQUEST['title'] . "%'";
        if ($_REQUEST['rstime'])
            $field[] = "dateline > " . strtotime($_REQUEST['rstime'] . ' 00:00:00');
        if ($_REQUEST['retime'])
            $field[] = "dateline < " . strtotime($_REQUEST['retime'] . ' 00:00:00');
        $table = & $this->_modelArticles->getTable();
        $page = isset($_GET['page']) ? (int) $_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
        $pager = & new FLEA_Helper_Pager($table, $page, 10, $field, 'dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*", $field);
        $this->_setBack();
        $pclass = $this->_modelClasses->getClass($catid);
        $subclasses = $this->_modelClasses->getSubClasses($pclass);
//        dump($this->dbo->log);
//		var_dump($rowset);
        $listTitle = '服务列表';
        include(TPL_DIR . '/BoServiceList.php');
    }

    // 添加
    function actionCreate() {
        $table = & $this->_modelArticles->getTable();
        $article = $this->_prepareData($table->meta);
        $this->_editArticle($article);
    }

    //保存
    function actionSave() {
        __TRY();
        $user = $this->getUser();
        if (!$_POST['article_id'])
            $_POST['dateline'] = time();
        $_POST['updated'] = time();
        $_POST['author'] = $user['ID'];
        $_POST['content'] = $_POST['spaw1'];
        $errorMessage = $this->_uploadPicture('postfile');
        if ($errorMessage && is_array($errorMessage))
            $_POST['image_src'] = $errorMessage['filename']; //上传图片
        else
            unset($_POST['image_src']);
        $arid = $this->_modelArticles->saveArticle($_POST);
        $ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
            return $this->_editArticle($_POST, $ex->getMessage());
        }
        redirect($this->_url('Index'));
    }

    /**
     * 修改文章
     */
    function actionEdit() {
        $article = $this->_modelArticles->getArticle($_GET['id']);
        $this->_editArticle($article);
    }

    /**
     * 显示文章信息编辑页面
     *
     * @param array $article
     * @param string $errorMessage
     */
    function _editArticle($article, $errorMessage = '') {
        $class_id = $article['class_id'] ? $article['class_id'] : $_REQUEST['class_id'];
        $table = & $this->_modelArticles->getTable();
        $pk = $table->primaryKey;
        $pclass = $this->_modelClasses->getClass($this->catid);
        $pclasses = $this->_modelClasses->getSubClasses($pclass);
        include(TPL_DIR . '/BoVideoEdit.php');
    }

    //删除资讯
    function actionRemove() {
        $this->_modelArticles->removeArticle($_GET['id']);
        $this->_goBack();
    }

}
